import 'package:flutter/material.dart';
import 'package:flutter_learning/utils/ly_app_color.dart';

class LYIndexPath {
  final int row;
  final int column;
  LYIndexPath({required this.row, required this.column});

  @override
  String toString() {
    return "row=$row column=$column";
  }
}

class SliverTableItem {
  final String? content;
  final bool canClick;
  final LYIndexPath indexPath;
  SliverTableItem({this.content, this.canClick = false, required this.indexPath});
}

class SliverTableRow {
  final List<SliverTableItem> items;
  SliverTableRow({required this.items});
}

class LYSliverTable extends StatelessWidget {
  final Map<int, TableColumnWidth> columnWidths;
  final List<String> titles;
  final List<SliverTableRow> rows;
  final Function(LYIndexPath)? onClick;
  const LYSliverTable(
      {super.key,
      required this.columnWidths,
      required this.titles,
      required this.rows,
      this.onClick});

  @override
  Widget build(BuildContext context) {
    List<TableRow> children = [];
    children.add(_makeTitleRow(titles));
    if (rows.isNotEmpty) {
      for (final e in rows) {
        children.add(_makeContentRow(e));
      }
    }
    return SliverToBoxAdapter(
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 15),
        decoration:
            BoxDecoration(color: const Color(0xFF1C2242), borderRadius: BorderRadius.circular(4)),
        clipBehavior: Clip.antiAlias,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Table(
            border: const TableBorder.symmetric(
                inside: BorderSide(color: Color(0xFF20274A), width: 1)), // 表格边框
            columnWidths: columnWidths,
            children: children,
          ),
        ),
      ),
    );
  }

  TableRow _makeContentRow(SliverTableRow row) {
    List<Widget> children = [];
    for (final e in row.items) {
      children.add(_makeContentItem(e));
    }
    return TableRow(
      decoration: const BoxDecoration(
        color: Color(0xFF141937),
      ),
      children: children,
    );
  }

  Widget _makeContentItem(SliverTableItem item) {
    return GestureDetector(
      onTap: () {
        onClick?.call(item.indexPath);
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: 50,
        alignment: Alignment.center,
        child: Text(
          item.content ?? '',
          style: TextStyle(
              fontSize: 14, color: item.canClick ? const Color(0xFF1690FF) : AppColor.whiteColor),
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
        ),
      ),
    );
  }

  TableRow _makeTitleRow(List<String> list) {
    List<Widget> children = [];
    for (final e in list) {
      children.add(_makeTitleRowItem(e));
    }
    return TableRow(
      decoration: const BoxDecoration(
        color: Color(0xFF141937),
      ),
      children: children,
    );
  }

  Widget _makeTitleRowItem(String title) {
    return Container(
      height: 50,
      alignment: Alignment.center,
      child: Text(
        title,
        style: TextStyle(fontSize: 14, color: AppColor.whiteColor),
      ),
    );
  }
}
